
/**
 * 将奇数在左，偶数在右
 * @param arr 乱序数组
 * @returns 排序后的数组
 */
export function sortOddEven(arr: number[]) {

    const len = arr.length;
    let right = len - 1;
    let i = 0;
    while (i <= right) {
        while (isEven(arr[i]) && i <= right) {
            while(isEven(arr[right]) && i <= right) {
                right--;
            }
            swap(arr, right, i);
            right--;
        }
        i++;
    }

    return arr;
}

/** 是否是偶数 */
function isEven (num: number) {
    return num % 2 === 0;
}

function swap (arr: number[], idx1: number, idx2: number) {
    const temp = arr[idx1];
    arr[idx1] = arr[idx2];
    arr[idx2] = temp;
}